package club.calong.sanniu.service;
// 团   队: 风讯网络
// 后   端: Dopa
// 开发时间: 2021/7/25 15:06

import club.calong.sanniu.http.request.SectionRequest;
import club.calong.sanniu.http.response.ListResponseData;
import club.calong.sanniu.pojo.Plate;
import club.calong.sanniu.pojo.Section;
import club.calong.sanniu.repository.PlateRepository;
import club.calong.sanniu.repository.SectionRepository;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.mail.Session;
import java.util.List;

@Service
public class SectionService {

    PlateRepository plateRepository;

    SectionRepository repository;

    @Autowired
    public SectionService(PlateRepository plateRepository, SectionRepository repository) {
        this.plateRepository = plateRepository;
        this.repository = repository;
    }

    public List<Section> list(Integer type){
        QueryWrapper<Section> wrapper = new QueryWrapper<>();
        if (type != null)
            wrapper.eq(Section.TYPE, type);
        return repository.selectList(wrapper);
    }

    public ListResponseData<Section> list (Integer current, Integer size, Integer type) {
        QueryWrapper<Section> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc(Plate.ID);
        if (type != null)
            wrapper.eq(Section.TYPE, type);
        Page<Section> page = repository.selectPage(new Page<>(current, size), wrapper);
        return new ListResponseData<>(page.getRecords(), page.getTotal());
    }

    public boolean create(Section section){

        return repository.insert(section) == 1;
    }

    public boolean update(Section section){

        return repository.updateById(section) == 1;
    }

    public boolean delete(Integer id){

        Section section = repository.selectById(id);
        QueryWrapper<Plate> wrapper = new QueryWrapper<>();
        wrapper.eq(Plate.SECTION, section.getSection());
        if (plateRepository.selectList(wrapper).size() > 0) {
            return false;
        } else {
            return repository.deleteById(id) == 1;
        }
    }
}
